<template>
  <div class="main">
    <div class="top">
      <el-button type="primary" size="small" @click="goAdd">新增</el-button>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[6,12,24]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalRow"
      ></el-pagination>
    </div>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column fixed type="index" label="#" width="50"></el-table-column>
      <el-table-column prop="title" label="文章标题" width="150"></el-table-column>
      <el-table-column prop="author" label="作者" width="120"></el-table-column>
      <el-table-column label="文章描述" width="500">
        <template slot-scope="scope">
          <div v-html="scope.row.content1"></div>
        </template>
      </el-table-column>
      <el-table-column label="封面图片" width="200">
        <template slot-scope="scope">
          <img :src="scope.row.cover" alt style="width:50px;height:50px" />
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button @click="del(scope.row.id)" type="text" size="small">删除</el-button>
          <el-button type="text" size="small" @click="edit(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { newslist, delNew } from "@/api/material/news";
export default {
  components: {},
  data() {
    return {
      tableData: [],
      totalPage: 0,
      totalRow: 0,
      currentPage: 1,
      pageSize: 6
    };
  },
  computed: {},
  created() {
    this.getlist();
  },
  mounted() {},
  methods: {
    getlist() {
      newslist({
        start: this.currentPage,
        limit: this.pageSize
      }).then(res => {
        console.log(res);
        this.tableData = res.data.list;
        this.totalPage = res.data.totalPage;
        this.totalRow = res.data.totalRow;
        console.log(this.tableData);
      });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.getlist();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
      console.log(this.currentPage);
      this.getlist();
    },
    del(id) {
      // console.log(id);
      this.$confirm("您确定要删除当前项吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        delNew({
          id
        }).then(res => {
          // console.log(res);
          var i = this.tableData.findIndex(el => el.id == id);
          this.tableData.splice(i, 1);
        });
      });
    },
    edit(item) {
      console.log(item);
      this.$router.push({ name: "newsAdd", params: { item: item, type: 1 } });
    },
    goAdd() {
      this.$router.push({ name: "newsAdd", params: { type: 0 } });
    }
    // goEdit() {
    //   this.$router.push({ name: "newsEdit", params: { type: 1 } });
    // }
  }
};
</script>

<style scoped lang="scss">
.main {
  .top{
    display: flex;
    justify-content: space-between;
  }
  .content {
    padding: 20px;
    box-sizing: border-box;
  }
}
</style>
